package com.code.background.drawable;

import android.content.Context;
import android.content.res.ColorStateList;
import android.content.res.TypedArray;

import com.code.background.R;
import com.code.background.common.MultiSelector;
import com.code.background.common.ResourceUtils;

public class MultiTextColorSelectorColorCreator implements ICreateColorState {

    private final TypedArray mSelectorTa;
    private final Context mContext;
    private int[][] mStates = new int[][]{};
    private int[] mColors = new int[]{};
    private int mIndex;
    public MultiTextColorSelectorColorCreator(Context context, TypedArray selectorTa) {
        this.mSelectorTa = selectorTa;
        this.mContext = context;
    }

    @Override
    public ColorStateList create() {
        mStates = new int[mSelectorTa.getIndexCount()][];
        mColors = new int[mSelectorTa.getIndexCount()];

        for (int i = 0; i < mSelectorTa.getIndexCount(); i++) {
            int attr = mSelectorTa.getIndex(i);

            if (attr == R.styleable.background_multi_selector_text_multi_text_selector1) {
                addState(attr);
            } else if (attr == R.styleable.background_multi_selector_text_multi_text_selector2) {
                addState(attr);
            } else if (attr == R.styleable.background_multi_selector_text_multi_text_selector3) {
                addState(attr);
            } else if (attr == R.styleable.background_multi_selector_text_multi_text_selector4) {
                addState(attr);
            } else if (attr == R.styleable.background_multi_selector_text_multi_text_selector5) {
                addState(attr);
            } else if (attr == R.styleable.background_multi_selector_text_multi_text_selector6) {
                addState(attr);
            }
        }
        return new ColorStateList(mStates, mColors);
    }

    private void addState(int attr) {
        String value = mSelectorTa.getString(attr);
        if(value != null){
            String[] vArray = value.split(",");
            if(vArray.length < 2){
                throw new IllegalArgumentException("Attributes and drawable must be set at the same time");
            }
            int color = 0;
            int[] attrId = new int[vArray.length - 1];
            for (int p = 0; p < vArray.length; p++){
                String attrStr = vArray[p];
                //取出资源id
                if(p == vArray.length - 1){
                    color = ResourceUtils.getColor(mContext, attrStr);
                    if(color == -1){
                        throw new IllegalArgumentException("cannot find color from the last attribute");
                    }
                }else {
                    MultiSelector multiSelector = MultiSelector.getMultiAttr(attrStr.replace("-", ""));
                    if(multiSelector == null){
                        throw new IllegalArgumentException("the attribute of multi_selector only support " +
                                "state_checkable, state_checked, state_enabled, state_selected, state_pressed, state_focused, " +
                                "state_hovered, state_activated");
                    }
                    if(attrStr.contains("-")){
                        attrId[p] = -multiSelector.id;
                    }else {
                        attrId[p] = multiSelector.id;
                    }
                }
            }
            mStates[mIndex] = attrId;
            mColors[mIndex] = color;
            mIndex++;
        }
    }
}
